package com.sanben.company.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sanben.common.util.BeanCopyUtils;
import com.sanben.company.data.dto.CompanyTeamDto;
import com.sanben.company.data.req.CompanyTeamReq;
import com.sanben.company.data.vo.CompanyTeamVo;
import com.sanben.company.entity.CompanyTeam;
import com.sanben.company.mapper.CompanyTeamMapper;
import com.sanben.company.service.ICompanyTeamService;
import com.sanben.demand.entity.Comment;
import com.sanben.demand.service.ICommentService;
import com.sanben.tag.service.ITagRelationshipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cyh
 * @description: 公司团队表 服务实现类
 * @date: 2020-08-11
 */
@Service
public class CompanyTeamServiceImpl extends ServiceImpl<CompanyTeamMapper, CompanyTeam> implements ICompanyTeamService {

    @Autowired
    private ITagRelationshipService  tagRelationshipService;

    @Autowired
    private ICommentService commentService;

    @Override
    public IPage<CompanyTeamVo> page(CompanyTeamReq req) {
        Page<CompanyTeamVo> page = new Page<>(req.getPageNum(), req.getPageSize());
        List<CompanyTeamVo> list = this.baseMapper.page(page, req);
        list.stream().forEach(each->{
            //标签
            each.setTagList(tagRelationshipService.getTagList(each.getId()));
        });
        page.setRecords(list);
        return page;
    }

    @Override
    public List<CompanyTeam> getHomeList() {
        LambdaQueryWrapper<CompanyTeam> lambdaQuery = new LambdaQueryWrapper<>();
        lambdaQuery.orderByDesc(CompanyTeam::getSort)
                .last("limit 4");
        return this.list(lambdaQuery);
    }

    @Override
    public List<CompanyTeam> answer() {
        LambdaQueryWrapper<Comment> commentQuery = new LambdaQueryWrapper<>();
        commentQuery.groupBy(Comment::getUserId)
                .last("limit 6");
        List<Integer> userIds = commentService.list(commentQuery).stream().map(p -> p.getId()).collect(Collectors.toList());
        LambdaQueryWrapper<CompanyTeam> companyTeamQuery = new LambdaQueryWrapper<>();
        companyTeamQuery.in(CompanyTeam::getUserId, userIds);
        return this.list(companyTeamQuery);
    }

    @Override
    public IPage<CompanyTeamVo> selectionCompanyTeam(CompanyTeamReq req) {

        return null;
    }

    @Override
    public Boolean save(CompanyTeamDto dto) {
        return this.save(BeanCopyUtils.copy(dto, CompanyTeam.class));
    }

    @Override
    public Boolean updateById(CompanyTeamDto dto) {
        return this.updateById(BeanCopyUtils.copy(dto, CompanyTeam.class));
    }


    @Override
    public Boolean deleteLogic(List<Integer> toIntList) {
        return this.removeByIds(toIntList);
    }

    @Override
    public CompanyTeam detail(Integer id) {
        return this.getById(id);
    }
}
